import json

# 读取txt文件，获取每行的URL和标题
def read_urls_from_txt(file_path):
    urls = []
    with open(file_path, 'r', encoding='utf-8') as file:
        for line in file:
            # 拆分每行的url和标题，假设每行只有一个空格分隔，并去掉空格
            parts = line.strip().split(' ', 1)
            if len(parts) == 2:
                url = parts[0].strip()  # 去掉URL中的空格
                title = parts[1].strip()  # 去掉标题中的空格
                urls.append((url, title))
    return urls

# 将数据转换为需要的JSON格式
def generate_json(urls, output_path):
    result = []
    for url, title in urls:
        data = {
            "mark": title,
            "url": url,
            "tab": "post",
            "earliest": "2015/1/1",
            "latest": "2025/12/12",
            "enable": True
        }
        result.append(data)

    # 将结果写入JSON文件
    with open(output_path, 'w', encoding='utf-8') as output_file:
        json.dump(result, output_file, ensure_ascii=False, indent=4)

# 主程序
def main():
    input_file = r'N:\trainData\洗稿\抖音采集\TikTokDownloader_V5.5_Windows_X64\urls.txt'  # 输入txt文件路径
    output_file = 'output.json'  # 输出json文件路径

    urls = read_urls_from_txt(input_file)
    generate_json(urls, output_file)
    print(f'JSON文件已生成：{output_file}')

if __name__ == '__main__':
    main()
